package com.test.mylotterysystem.service.ActivityStatusChange.Operator;

import com.test.mylotterysystem.dao.DO.ActivityUserDO;
import com.test.mylotterysystem.dao.Mapper.ActivityUserMapper;
import com.test.mylotterysystem.service.DTO.ConvertActivityStatusDTO;
import com.test.mylotterysystem.service.serviceEnum.ActivityUserStatusEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class ActivityUserOperator extends AbstractOperator{

    @Autowired
    private ActivityUserMapper activityUserMapper;

    @Override
    public Integer sequence() {
        return 1;
    }

    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        List<ActivityUserDO> activityUserDOS = activityUserMapper.selectByAUIds(convertActivityStatusDTO.getActivityId(), convertActivityStatusDTO.getUserIds());
        for (ActivityUserDO activityUserDO : activityUserDOS) {
            if (activityUserDO.getStatus().equalsIgnoreCase(ActivityUserStatusEnum.COMPLETE.name())) {
                return false;
            }
        }
        return true;
    }

    @Override
    public Boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        try {
            activityUserMapper.updateStatus(convertActivityStatusDTO.getActivityId()
                    , convertActivityStatusDTO.getUserIds(), ActivityUserStatusEnum.COMPLETE.name());
            return true;
        } catch (Exception e) {
            log.error("转换失败");
            return false;
        }
    }
}
